﻿package domino

{
  import adobe.utils.CustomActions;
  import Box2D.Common.Math.b2Vec2;
  import Box2D.Dynamics.b2Body;
  import Box2D.Dynamics.b2World;

  import flash.display.DisplayObject;
  import flash.events.EventDispatcher;
  import flash.geom.Point;
  import flash.geom.Vector3D;
  //import flash.geom
  
  
  

  
public class actor extends EventDispatcher
{
	 var _body:b2Body;
     var _costume:DisplayObject;
     var pointImpulse:Point;
	 var angle:int=0;
	 
  public function actor(body:b2Body,costume:DisplayObject){

   _body = body;
   _body.SetUserData(this);
   //trace(_body.GetJointList().GetBody2());
   //trace(_body.GetJointList().GetBody1());
  // trace(_body.GetWorldCenter().x+" center "+ _body.GetWorldCenter().y);
  trace(_body.IsSleeping());
   _costume = costume;
  }
  
 
  
  public function update() {
	  updateBody();
	  updateOther();
	}
//////////////////////////////////////////////////	
  private function updateOther(){
	 if ((_costume.x > 800) || (_costume.y >600)||(_costume.x < -200) || (_costume.y <-200)){
	  dispatchEvent(new OutOfScreen(OutOfScreen.OUT_DOMIN));
	  trace(this + " should be remove");
	 }
  }
  /////////////////////////////////////////////
  //////////////////////////////////////////////
  public function rotate(angl:int) {
	  angle -= angl;
	 // var a = 2 * Math.PI / (angle % 18) ;
	  trace(angle);
  
	  _body.SetXForm(new b2Vec2(_body.GetPosition().x, _body.GetPosition().y), angle*Math.PI/180);
	  _costume.rotation=angle;
	  }
	///////////////////////////////////////////////  
  private function updateBody():void{
    _costume.x = _body.GetPosition().x*30;
    _costume.y = _body.GetPosition().y*30;
    _costume.rotation = _body.GetAngle() * 180 / Math.PI;
  }
  
//////////////////////////////////////////////
 public function removeActor(_world:b2World):void {
	   //remove sprite
	  trace("delete");
	   if(_body.GetShapeList().GetUserData() is b2Body)
	    _world.DestroyBody(_body.GetShapeList().GetUserData());;
	   
	   _world.DestroyBody(_body);
	   
	  // this.
	   //remove b2Body
	   _costume.parent.removeChild(_costume);
	  // trace("up1");
	   
	  }
////////////////////////////////////////
	  public function GetArrowAngle(_pointImpulse:Point,_pointCenter:Point):Number {
		 //trace("1");
		  var Vector1:Vector3D = new Vector3D(0,1,0);
	      
		  var Vector2:Vector3D = new Vector3D(_pointImpulse.x -_pointCenter.x,_pointImpulse.y -_pointCenter.y,0);
	      var alpha;
		  var alpha2;
		 		  
	      Vector1.normalize();
		  Vector2.normalize();
		 //trace("2");
		 alpha = Math.acos((Vector1.x * Vector2.x + Vector1.y * Vector2.y) / Math.sqrt(( Vector1.x * Vector1.x + Vector1.y * Vector1.y) * (Vector2.x * Vector2.x + Vector2.y * Vector2.y)) );
		 //trace("3");
		 if(Vector1>Vector2)
		   return alpha;
		  else
		   return -alpha;
		  }
	  
	
	    
/////////////////////////////////////////////	

 public function move(x:int,y:int) { 
	  _body.SetXForm(new b2Vec2(_body.GetPosition().x+x/30, _body.GetPosition().y+y/30), _body.GetAngle());
	  _costume.x +=x ;
	  _costume.y +=y ;
	 }
  
  public function moveDomino(_pointImpulse:Point) {
	    
	   var impulse:b2Vec2;
	   var pointImpulse:b2Vec2;
	   
	   var length:int;
	   var angle:int;
	    
	   pointImpulse = new b2Vec2(_pointImpulse.x / 30, _pointImpulse.y / 30);
	   length = Point.distance(_pointImpulse, new Point(_body.GetPosition().x * 30, _body.GetPosition().y * 30));  		
       
		//trace("angle is "+Vector3D.angleBetween(normalVector, Vector2)); 
	    //trace("lenght is "+length/30); 
		
		if(pointImpulse.x>_body.GetPosition().x)	
	      impulse = new b2Vec2(-length/3, 0);
		else
		  impulse= new b2Vec2(length/3, 0);

		_body.ApplyImpulse(impulse,pointImpulse);
	}
			
  public function getPos():b2Vec2 {
	  var centerPoint:b2Vec2 = new b2Vec2(_body.GetPosition().x, _body.GetPosition().y);
	  //trace ("hello");
	  return centerPoint;
	}

}

 

}
